package com.threadPool;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class DemoFutureTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {


        ThreadPoolExecutor threadPoolExecutor =new ThreadPoolExecutor(2, 5,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>());

        List<Future<String>> list = new ArrayList<>();

        Future submitA = threadPoolExecutor.submit(new DemoFutureA());

        list.add(submitA);

        Future submitB = threadPoolExecutor.submit(new DemoFutureB());

        list.add(submitB);

        List<String> resultList = new ArrayList<>();

        long Start = System.currentTimeMillis();
        System.out.println("====="+Start);

        for (Future<String> future : list){
            long time1 = System.currentTimeMillis();
            System.out.println("future.get()开始==="+time1);
            //String s = future.get();
            //设置等待时间:
            String s= null;
            try {
                s = future.get(5, TimeUnit.SECONDS);
            } catch (TimeoutException e) {
                e.printStackTrace();


            }
            long time2 = System.currentTimeMillis();
            System.out.println("future.get()结束"+(time2-time1)+"==="+s);
            resultList.add(s);
        }

        System.out.println("====="+(System.currentTimeMillis()-Start));

        System.out.println(resultList);

        System.out.println("=====总耗时=="+Thread.currentThread().getName());

        threadPoolExecutor.shutdown();
    }
}
